package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.GroupListAdapter;
import com.hengxinguotong.zhihuichengjian.bean.GroupBean;
import com.hengxinguotong.zhihuichengjian.bean.GroupListRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 班组管理
 * Created by liux on 2017/3/2.
 */

public class GroupListActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.group_lv)
    PullToRefreshListView groupLv;
    @BindView(R.id.add)
    HXTextView add;

    private GroupListAdapter groupListAdapter;
    private List<GroupBean> groupBeens;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grouplist);
        ButterKnife.bind(this);
        groupLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        addListener();
        getGroupList(getResources().getString(R.string.loading));
        if (hasAdd()) {
            add.setVisibility(View.VISIBLE);
        }
    }


    private void addListener() {
        ivBack.setOnClickListener(this);
        add.setOnClickListener(this);
        groupLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GroupListActivity.this, GroupActivity.class);
                intent.putExtra("authority", authority);
                intent.putExtra("teamId", groupBeens.get(position - 1).getId());
                intent.putExtra("teamName", groupBeens.get(position - 1).getName());
                startActivityForResult(intent, 1);
            }
        });

        groupLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getGroupList(null);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getGroupList(null);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.add:
//                ActivityJumper.toAddGroupActivity(AddGroupActivity.ADDTYPE, this, null, null);
                Intent intent = new Intent(this, AddTeamActivity.class);
                startActivityForResult(intent, 1);
                break;
        }

    }


    private void getGroupList(String alert) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("constructionId", SPUtil.getString(this, "constructionid"));
        Utils.requestData(alert, this, "/team/queryTeamList/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (groupLv.isRefreshing()) {
                    groupLv.onRefreshComplete();
                }
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    GroupListRes groupListRes = gson.fromJson(json, GroupListRes.class);
                    addData(groupListRes.getValue());
                }
            }

            @Override
            public void onFailure(String json) {
                if (groupLv.isRefreshing()) {
                    groupLv.onRefreshComplete();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1 && groupLv != null) {
            groupLv.setRefreshing();
        }
    }

    private void addData(List<GroupBean> datas) {
        if(datas.size()==0){
            showEmptyView(groupLv);
        }else{
            dismissEmptyView();
        }
        groupBeens = datas;
        groupListAdapter = new GroupListAdapter(this, groupBeens);
        groupLv.setAdapter(groupListAdapter);

    }
}
